package com.ywl.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywl.common.CustomException;
import com.ywl.common.R;
import com.ywl.entity.Banner;
import com.ywl.service.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  前端控制器
 * @author ywl
 * @since 2022-08-26
 */
@RestController
@RequestMapping("/banner")
@Api(tags = "轮播图控制器")
public class BannerController {
    @Autowired
    private BannerService bannerService;

    @ApiOperation("获取轮播图，图片数量为5")
    @GetMapping
    @Cacheable(value = "bannerCache",key ="'banner'")
    public R<List<String>> getBanner(){
        List<Banner> banners = bannerService.get();
        if(banners == null){
            throw new CustomException("请求失败");
        }
        List<String> imgUrls = new ArrayList<>();
        imgUrls = banners.parallelStream()
                .distinct()
                .map(banner -> {
                    String imgUrl = banner.getImgUrl();
                    return imgUrl;
                }).collect(Collectors.toList());
        return R.success(imgUrls);
    }
}

